package orchestration.android.execution.tasks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.util.Log;

import orchestration.android.RunExecutionActivity;
import orchestration.android.execution.exceptions.TaskExecutionException;
import orchestration.android.parcelable.ParcelableProperty;
import orchestration.businessprocess.execution.Property;
import orchestration.persistence.exceptions.UnsupportedTypeException;
import orchestration.utils.PropertyUtils;


/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public abstract class AndroidTask {
	private static final String TAG="AndroidTask";
	
	/**
	 * @uml.property  name="id"
	 */
	private String id;
	/**
	 * @uml.property  name="input"
	 */
	private HashMap<String,ParcelableProperty> input;
	/**
	 * @uml.property  name="output"
	 */
	private HashMap<String,ParcelableProperty> output;
	/**
	 * @uml.property  name="mappings"
	 */
	private Map<String,String> mappings;
	public AndroidTask(String id) {
		this.id=id;
		this.input= new HashMap<String,ParcelableProperty>();
		this.output= new HashMap<String,ParcelableProperty>();
		this.mappings= new HashMap<String, String>();
	}
	/**
	 * @param input
	 * @uml.property  name="input"
	 */
	public void setInput(HashMap<String,ParcelableProperty> input) {
		this.input = input;
	}
	/**
	 * @return
	 * @uml.property  name="input"
	 */
	public HashMap<String, ParcelableProperty> getInput() {
		return input;
	}
	/**
	 * @param output
	 * @uml.property  name="output"
	 */
	public void setOutput(HashMap<String,ParcelableProperty> output) {
		this.output = output;
	}
	/**
	 * @return
	 * @uml.property  name="output"
	 */
	public HashMap<String,ParcelableProperty> getOutput() {
		return output;
	}
	/**
	 * @param id
	 * @uml.property  name="id"
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * @return
	 * @uml.property  name="id"
	 */
	public String getId() {
		return id;
	}
	/**
	 * @return
	 * @uml.property  name="mappings"
	 */
	public Map<String, String> getMappings() {
		return mappings;
	}
	/**
	 * @param mappings
	 * @uml.property  name="mappings"
	 */
	public void setMappings(Map<String, String> mappings) {
		this.mappings = mappings;
	}
	public String getMappedName(String artifactName){
		Log.i(TAG,"looking for alias for " + artifactName + " will find "+ mappings.get(artifactName));
		return mappings.get(artifactName);
	}
	public String getArtifactName(String mappedName){
		if(mappedName==null)
			return null;
		for(String mappingKey : mappings.keySet()){
			if(mappings.get(mappingKey).equals(mappedName))
				return mappingKey;
		}
		return mappedName;
	}
	
	protected void fillIntentExtras(RunExecutionActivity rea, Intent intentToFill) throws UnsupportedTypeException {
		for(Property prop : input.values()){
				PropertyUtils.writePropertyValueToIntent(intentToFill, prop,getMappedName(prop.getName()));
		}
	}
	protected void saveTaskOutput(RunExecutionActivity rea, Intent result) throws UnsupportedTypeException {
		if(result!=null){
			List<Property> properties = new ArrayList<Property>();
			for(Property prop : output.values()){
					PropertyUtils.readPropertyValueFromIntent(result, prop,getMappedName(prop.getName()));
					properties.add(prop);
					rea.saveProperties(properties);				
				
			}
		}
	}
	
	public abstract void onExecute(RunExecutionActivity atl) throws TaskExecutionException;
	
}
